/*
    对helper.hpp文件中FileHelper类的测试
*/
#include "../../common/helper.hpp"

void test01()
{
    std::string file = "./makefile";
    mq::FileHelper file_helper(file);
    // 1. 判断文件是否存在
    if (!file_helper.exists())
        std::cout << file << " file not exists" << std::endl;
    else
        std::cout << file << " file exists" << std::endl;

    // 2. 获取文件大小
    std::cout << file << " file size: " << file_helper.size() << std::endl;
}

void test02()
{
    std::string src_file = "./makefile";
    std::string dest_file = "./a/b/c/makefile";
    // 1. 获取父目录
    std::string pather_dir = mq::FileHelper::parentDir(dest_file);

    // 2. 创建目录
    mq::FileHelper::createDir(pather_dir);

    // 3. 创建文件
    mq::FileHelper::createFile(dest_file);

    // 4. 读取源文件
    mq::FileHelper read_helper(src_file);
    size_t size = read_helper.size();
    std::string buf(size, '\0');
    read_helper.read(&buf[0], 0, size);

    // 5. 写入文件
    mq::FileHelper write_helper(dest_file);
    write_helper.write(&buf[0], 0, buf.size());
}

void test03()
{
    std::string file1 = "./a/b/c/makefile";
    // 1. 从指定位置读取文件
    char buf[1024] = {0};
    mq::FileHelper read_helper(file1);
    read_helper.read(buf, 5, 7);

    // 2. 从指定位置写文件
    size_t length = 10;
    mq::FileHelper write_helper(file1);
    size_t size = write_helper.size();
    write_helper.write((char*)&length, 0, sizeof(size_t));

    // 3. 从指定位置读
    size_t res = 0;
    write_helper.read((char*)&res, 0, sizeof(size_t));
}

void test04()
{
    // 1. 文件重命名
    std::string file = "./a/b/c/makefile";
    std::string new_file_name = "./a/b/c/makefile.bak";
    mq::FileHelper file_helper(file);
    file_helper.rename(new_file_name);
}

void test05()
{
    // 1. 截断文件测试
    std::string new_file_name = "./a/b/c/makefile.bak";
    mq::FileHelper file_helper(new_file_name);
    file_helper.truncate(5);

    // 1. 删除文件
    mq::FileHelper::removeFile(new_file_name);

    // 2. 删除目录
    mq::FileHelper::removeDir("./a");
}
int main()
{
    test01();
    test02();
    test03();
    test04();
    test05();
    return 0;
}